/*===========================================================================+
 |   Copyright (c) 2001, 2005 Oracle Corporation, Redwood Shores, CA, USA    |
 |                         All rights reserved.                              |
 +===========================================================================+
 |  HISTORY                                                                  |
 +===========================================================================*/
package xxub.oracle.apps.xxierp.travel.webui;

import com.sun.java.util.collections.HashMap;

import java.io.Serializable;

import oracle.apps.fnd.common.VersionInfo;
import oracle.apps.fnd.framework.OAApplicationModule;
import oracle.apps.fnd.framework.OAException;
import oracle.apps.fnd.framework.OAViewObject;
import oracle.apps.fnd.framework.webui.OAControllerImpl;
import oracle.apps.fnd.framework.webui.OAPageContext;
import oracle.apps.fnd.framework.webui.OAWebBeanConstants;
import oracle.apps.fnd.framework.webui.beans.OAWebBean;
import oracle.apps.fnd.framework.webui.beans.form.OASubmitButtonBean;
import oracle.apps.fnd.framework.webui.beans.message.OAMessageChoiceBean;
import oracle.apps.fnd.framework.webui.beans.message.OAMessageDateFieldBean;
import oracle.apps.fnd.framework.webui.beans.message.OAMessageTextInputBean;

import oracle.jbo.Row;
import oracle.jbo.ViewObject;


/**
 * Controller for ...
 */
public class SearchPGCO extends OAControllerImpl {
    public static final String RCS_ID = "$Header$";
    public static final boolean RCS_ID_RECORDED = 
        VersionInfo.recordClassVersion(RCS_ID, "%packagename%");

    /**
     * Layout and page setup logic for a region.
     * @param pageContext the current OA page context
     * @param webBean the web bean corresponding to the region
     */
    public void processRequest(OAPageContext pageContext, OAWebBean webBean) {
        super.processRequest(pageContext, webBean);
        System.out.println("LoginName:" + pageContext.getUserName());

        OAMessageTextInputBean trvlReqNumber = 
            (OAMessageTextInputBean)webBean.findChildRecursive("TravelConReqNo");
        OAMessageDateFieldBean trvlReqDate = 
            (OAMessageDateFieldBean)webBean.findChildRecursive("TravelConReqDate");
        OAMessageChoiceBean status = 
            (OAMessageChoiceBean)webBean.findChildRecursive("StatusId");
        OAMessageTextInputBean amount = 
            (OAMessageTextInputBean)webBean.findChildRecursive("AdvanceAmount");
        trvlReqNumber.setReadOnly(true);
        trvlReqDate.setReadOnly(true);
        status.setReadOnly(true);
        amount.setReadOnly(true);

        String loginUserSup = pageContext.getUserName();
        String respName = pageContext.getResponsibilityName();
        int respId = pageContext.getResponsibilityId();
        String superviser = "THSMITH";
        if (loginUserSup.equals(superviser) || 
            respName.equalsIgnoreCase("Manager Self-Service")) { //Approver Code

            OASubmitButtonBean createBtn = 
                (OASubmitButtonBean)webBean.findChildRecursive("create");
            createBtn.setRendered(false);

            OAApplicationModule am = 
                (OAApplicationModule)pageContext.getApplicationModule(webBean);
            Serializable[] loginUser = { pageContext.getUserName() };
            am.invokeMethod("queryLoginUser", loginUser);

            OAViewObject empVO = 
                (OAViewObject)am.findViewObject("EmployeeDtlsVO1");
            Row r[] = empVO.getAllRowsInRange();
            String appFullName = (String)r[0].getAttribute("ReqFullName");
            String appPersonId = 
                (String)r[0].getAttribute("PersonId").toString();

        } else {
            OAApplicationModule am = 
                (OAApplicationModule)pageContext.getApplicationModule(webBean);
            Serializable[] loginUser = { pageContext.getUserName() };
            am.invokeMethod("queryLoginUser", loginUser);
        }


    }

    /**
     * Procedure to handle form submissions for form elements in
     * a region.
     * @param pageContext the current OA page context
     * @param webBean the web bean corresponding to the region
     */
    public void processFormRequest(OAPageContext pageContext, 
                                   OAWebBean webBean) {
        super.processFormRequest(pageContext, webBean);
        OAApplicationModule am = 
            (OAApplicationModule)pageContext.getApplicationModule(webBean);
        String Vstatus = "";
        String refNo = "";
        String lastTrscnDt = "";
        String lastTrscnDtAcConv = "";
        String concessionAmt = "";

        if ("Create".equalsIgnoreCase(pageContext.getParameter(EVENT_PARAM)) || 
            "View".equals(pageContext.getParameter(EVENT_PARAM))) {
            OAViewObject empVO = 
                (OAViewObject)am.findViewObject("EmployeeDtlsVO1");
            Row r[] = empVO.getAllRowsInRange();
            String fullName = (String)r[0].getAttribute("ReqFullName");
            String department = (String)r[0].getAttribute("Organization");
            String personId = (String)r[0].getAttribute("PersonId").toString();
            String deptId = (String)r[0].getAttribute("DeptId").toString();
            if ("Create".equals(pageContext.getParameter(EVENT_PARAM)))
                Vstatus = "Create";
            else if ("View".equals(pageContext.getParameter(EVENT_PARAM)))
                Vstatus = "View";

            if (Vstatus == "Create") {
                // pageContext.putParameter("paramStatusId", "TC01");

                OAViewObject hisVo = 
                    (OAViewObject)am.findViewObject("XxierpTravelConcessionHistoryVO1");
                Serializable[] persId = { personId };
                String returnVal;
                returnVal = 
                        am.invokeMethod("requestInProgress", persId).toString();
                if (returnVal.equals("N")) {

                    /**Allow the Person to create the new Request*/

                    pageContext.putParameter("paramStatusId", "TC01");
                    hisVo.setWhereClause(null);
                    hisVo.setWhereClause("PERSON_ID=" + personId);
                    hisVo.setOrderByClause("TRAVEL_CON_REQ_DATE DESC");
                    hisVo.executeQuery();
                    Row prevDateRow;
                    prevDateRow = hisVo.first();
                    String date;
                    date = 
prevDateRow.getAttribute("TravelConReqDate").toString();
                    pageContext.putParameter("PreviousTranscDate", date);

                    HashMap hm = new HashMap();
                    hm.put("Status", Vstatus);
                    hm.put("pPersonId", personId);
                    hm.put("pFullName", fullName);
                    hm.put("pOrganization", department);
                    hm.put("pDeptId", deptId);
                    pageContext.setForwardURL("OA.jsp?page=/xxub/oracle/apps/xxierp/travel/webui/RequestPG", 
                                              null, 
                                              OAWebBeanConstants.KEEP_MENU_CONTEXT, 
                                              null, hm, true, 
                                              OAWebBeanConstants.ADD_BREAD_CRUMB_YES, 
                                              OAWebBeanConstants.IGNORE_MESSAGES);
                } else {
                    OAException successMessage = 
                        new OAException("You can't Create the Request while Previous Requests are Pending!", 
                                        OAException.ERROR);
                    pageContext.putDialogMessage(successMessage);
                }
            } else if (Vstatus == "View") {
                HashMap hm = new HashMap();
                hm.put("Status", Vstatus);
                hm.put("pPersonId", personId);
                hm.put("pFullName", fullName);
                hm.put("pOrganization", department);
                hm.put("pDeptId", deptId);
                pageContext.setForwardURL("OA.jsp?page=/xxub/oracle/apps/xxierp/travel/webui/RequestPG", 
                                          null, 
                                          OAWebBeanConstants.KEEP_MENU_CONTEXT, 
                                          null, hm, true, 
                                          OAWebBeanConstants.ADD_BREAD_CRUMB_YES, 
                                          OAWebBeanConstants.IGNORE_MESSAGES);
            }
        }
    }
}
